package com.util;

import com.common.ServiceException;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class OtpUtils {
    /**
     * 生成随机OTP 范围在1000-10000
     *
     * @return
     */
    public static String generateOtp() {
        try {
            SecureRandom secureRandom = SecureRandom.getInstanceStrong();

            int rand = secureRandom.nextInt(9000) + 1000;
            return rand + "";
        } catch (NoSuchAlgorithmException e) {
            throw new ServiceException("20001", e.getMessage());
        }
    }
}
